package com.lancoo.rxretrofitlibrary.Api;

import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.lang.ref.SoftReference;

import retrofit2.Retrofit;
import rx.Observable;

/**
 * Created by tangminglong on 17/5/11.
 */

public abstract class BaseApi {
    public static String testLayer = UserInfo.StageNo;
    //rx生命周期管理
    private SoftReference<RxAppCompatActivity> rxAppCompatActivity;
    //rx生命周期管理
    private SoftReference<RxFragment> rxFragment;
    /*回调*/
    private HttpOnNextListener listener;
    /*是否能取消加载框*/
    private boolean cancel;
    /*是否显示加载框*/
    private boolean showProgress;
    /*是否需要缓存处理*/
    private boolean cache;

    /*方法-如果需要缓存必须设置这个参数；不需要不用設置*/
    private String mothed;
    /*超时时间-默认6秒*/
    private int connectionTime = 10;
    /*有网情况下的本地缓存时间默认60秒*/
    private int cookieNetWorkTime = 60;
    /*无网络的情况下本地缓存时间默认30天*/
    private int cookieNoNetWorkTime = 24 * 60 * 60 * 30;
    /* 失败后retry次数*/
    private int retryCount = 3;
    /*失败后retry延迟*/
    private int retryDelay = 1000;
    /*失败后retry叠加延迟*/
    private int retryIncreaseDelay = 500;

    private boolean isActivity;//判断传进来的是否是Activity还是Fragment


    public BaseApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) {
        setIsActivity(true);
        setListener(listener);
        setRxAppCompatActivity(rxAppCompatActivity);
        setShowProgress(false);//默认不显示加载框
        setCache(false);//默认不缓存
    }

    public BaseApi(HttpOnNextListener listener, RxFragment rxFragment) {
        setIsActivity(false);
        setListener(listener);
        setRxFragment(rxFragment);
        setRxAppCompatActivity((RxAppCompatActivity) rxFragment.getActivity());
        setShowProgress(false);//默认不显示加载框
        setCache(false);//默认不缓存
    }




    /**
     * 设置参数
     *
     * @param retrofit
     * @return
     */
    public abstract Observable getObservable(Retrofit retrofit);


    public int getCookieNoNetWorkTime() {
        return cookieNoNetWorkTime;
    }

    public void setCookieNoNetWorkTime(int cookieNoNetWorkTime) {
        this.cookieNoNetWorkTime = cookieNoNetWorkTime;
    }

    public int getCookieNetWorkTime() {
        return cookieNetWorkTime;
    }

    public void setCookieNetWorkTime(int cookieNetWorkTime) {
        this.cookieNetWorkTime = cookieNetWorkTime;
    }

    public String getMothed() {
        return mothed;
    }

    public int getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(int connectionTime) {
        this.connectionTime = connectionTime;
    }

    public void setMothed(String mothed) {
        this.mothed = mothed;
    }



    //TODO
    public String getUrl() {
        return "" + mothed;
    }

    public boolean isCache() {
        return cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    public HttpOnNextListener getListener() {
        return listener;
    }

    public void setListener(HttpOnNextListener listener) {
        this.listener =  listener;
    }

    public int getRetryCount() {
        return retryCount;
    }


    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public int getRetryDelay() {
        return retryDelay;
    }

    public void setRetryDelay(int retryDelay) {
        this.retryDelay = retryDelay;
    }

    public int getRetryIncreaseDelay() {
        return retryIncreaseDelay;
    }

    public void setRetryIncreaseDelay(int retryIncreaseDelay) {
        this.retryIncreaseDelay = retryIncreaseDelay;
    }

    public void setRxAppCompatActivity(RxAppCompatActivity rxAppCompatActivity) {
        this.rxAppCompatActivity = new SoftReference(rxAppCompatActivity);
    }

    /*
         * 获取当前rxActivity生命周期
         * @return
         */
    public RxAppCompatActivity getRxAppCompatActivity() {
        return rxAppCompatActivity.get();
    }

    /**
     * 设置当前rxFragment生命周期
     * @return
     */
    public RxFragment getRxFragment() {
        return rxFragment.get();
    }

    public void setRxFragment(RxFragment rxFragment) {
        this.rxFragment = new SoftReference(rxFragment);
    }

    public boolean isActivity() {
        return isActivity;
    }

    public void setIsActivity(boolean activity) {
        isActivity = activity;
    }


    /***************************************其他设置*******************************/
     /**
     * 加载框的标题
     */
    private String progressTitle;
    /**
     * 加载框的内容
     */
    private String progressContent;

    public String getProgressTitle() {
        return progressTitle;
    }

    public void setProgressTitle(String progressTitle) {
        this.progressTitle = progressTitle;
    }

    public String getProgressContent() {
        return progressContent;
    }

    public void setProgressContent(String progressContent) {
        this.progressContent = progressContent;
    }

    /***************************************需要统一处理的接口方法(如：tokean)*******************************/


    private static String responseTime;//做题服务器相应时间
    private static long loginResponseTime;//本地的时间

    public static String getResponseTime() {
        return responseTime;
    }

    public static void setResponseTime(String responseTime) {
        BaseApi.responseTime = responseTime;
    }

    public static long getLoginResponseTime() {
        return loginResponseTime;
    }

    public static void setLoginResponseTime(long loginResponseTime) {
        BaseApi.loginResponseTime = loginResponseTime;
    }



    /**
     * 获取tokean统一接口
     * @param retrofit
     * @return
     */
   /* public Observable getObservableTokean(Retrofit retrofit){
        HttpService httpService = retrofit.create(HttpService.class);
        return httpService.getAllVedioBy(true,getTokean());
    }*/

}
